import java.sql.*;
public class JDBC {
    private Connection conn;
    static{
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("no");
        }
    }
    public static Connection getConnection(){
        Connection conn = null;
        try {
            String url="jdbc:mysql://localhost:3306/1?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
            String username ="root";
            String password="123456";
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            System.out.println("获取连接失败！");
            e.printStackTrace();
        }
        return conn;
    }
    public static int executeUpdate(String sql){
        Connection conn = getConnection();
        PreparedStatement ps = null;
        int rows=0;
        try {
            ps = getConnection().prepareStatement(sql);
            rows = ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("no");
        }
        return rows;

    }

    public static void closeConnection(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
                System.out.println("连接已关闭");
            }
        } catch (SQLException e) {
            System.out.println("no");
        }
    }
}
